<?php
/* $Id: PriceFormatterStore.php v 1.01 2007/03/03
   an object to store the price breaks and products_quantity of a product once queried by the 
   class PriceFormatter.php to avoid it being queried more than once (5 times for an article in the shopping basket!)
   and tep_get_stock to be executed for each product on the page shopping_cart.php
	 
   osCommerce, Open Source E-Commerce Solutions
   http://www.oscommerce.com

   Copyright (c) 2006 osCommerce

  Released under the GNU General Public License
*/

class PriceFormatterStore {
	var $pricebreaks = array();

	function PriceFormatterStore() {
		global $cart, $languages_id;
		if (isset($_SESSION['sppc_customer_group_id']) && $_SESSION['sppc_customer_group_id'] != '0') {
		  $this->cg_id = (int)$_SESSION['sppc_customer_group_id'];
		} else {
		  $this->cg_id = 0;
		}

		if (is_object($cart)) {
			$product_id_list = $cart->get_product_id_list();
			if (tep_not_null($product_id_list)) {
				// get rid of attributes first
				$product_id_list_array = array();
				$product_id_list_temp_array = explode(",", $product_id_list);
				foreach ($product_id_list_temp_array as $key => $value) {
          $valid_value = tep_get_prid($value);
          if (tep_not_null($valid_value)) {
					  $product_id_list_array[] = $valid_value;
          }
				}
				unset($product_id_list);
				$product_id_list = implode(",", $product_id_list_array);
				// now do one query for all products in the shopping basket
				    $sql = "select pd.products_name, p.products_model, p.products_image, p.products_id," .
        " p.products_price, p.products_weight, p.products_quantity, " .
        " p.products_price1, p.products_price2, p.products_price3, p.products_price4, " . 
        " p.products_price5, p.products_price6, p.products_price7, p.products_price8, " .
        " p.products_price1_qty, p.products_price2_qty, p.products_price3_qty, p.products_price4_qty, " .
        " p.products_price5_qty, p.products_price6_qty, p.products_price7_qty, p.products_price8_qty, " .
        " p.products_qty_blocks, p.products_tax_class_id," .
        " NULL as specials_new_products_price" .
        " from " . TABLE_PRODUCTS_DESCRIPTION . " pd," .
        "      " . TABLE_PRODUCTS . " p" .
        " where p.products_status = '1'" .
        "   and p.products_id in (" . $product_id_list . ")" .
        "   and pd.products_id = p.products_id " .
        "   and pd.language_id = '". (int)$languages_id ."'";
				    $product_info_query = tep_db_query($sql);
						$no_of_listings = tep_db_num_rows($product_info_query);
						while ($_product_info = tep_db_fetch_array($product_info_query)) {
							$product_info[] = $_product_info;
						}

// get all product prices for products with the particular customer_group_id
// however not necessary for customer group  0 (retail)
						if ($this->cg_id != '0') {
              // first remove all the retail prices and quantity blocks
              foreach ($product_info as $key => $sub_array_product_info) {
                for ($i = 1 ; $i < 9 ; $i++) {
                   $product_info[$key]['products_price'.$i.''] = '0.0000';
                   $product_info[$key]['products_price'.$i.'_qty'] = '0';
                 } 
		             $product_info[$key]['products_qty_blocks'] = '1';
              } // end foreach ($product_info as $key => $sub_array_product_info)
			  $pg_query = tep_db_query("select pg.products_id, customers_group_price, products_price1," .
              " products_price2, products_price3, products_price4, products_price5, products_price6," .
              " products_price7, products_price8, products_price1_qty, products_price2_qty, products_price3_qty," .
              " products_price4_qty, products_price5_qty, products_price6_qty, products_price7_qty," .
              " products_price8_qty, products_qty_blocks from " . TABLE_PRODUCTS_GROUPS . " pg where " .
              " pg.products_id in (" . $product_id_list . ") and pg.customers_group_id = '" . $this->cg_id . "' ");

			  while ($pg_array = tep_db_fetch_array($pg_query)) {
							$new_prices[] = array ('products_id' => $pg_array['products_id'], 'products_price' => $pg_array['customers_group_price'], 
              'products_price1' => $pg_array['products_price1'], 'products_price1_qty' => $pg_array['products_price1_qty'],
              'products_price2' => $pg_array['products_price2'], 'products_price2_qty' => $pg_array['products_price2_qty'],
              'products_price3' => $pg_array['products_price3'], 'products_price3_qty' => $pg_array['products_price3_qty'],
              'products_price4' => $pg_array['products_price4'], 'products_price4_qty' => $pg_array['products_price4_qty'],
              'products_price5' => $pg_array['products_price5'], 'products_price5_qty' => $pg_array['products_price5_qty'],
              'products_price6' => $pg_array['products_price6'], 'products_price6_qty' => $pg_array['products_price6_qty'],
              'products_price7' => $pg_array['products_price7'], 'products_price7_qty' => $pg_array['products_price7_qty'],
              'products_price8' => $pg_array['products_price8'], 'products_price8_qty' => $pg_array['products_price8_qty'],
              'products_qty_blocks' => $pg_array['products_qty_blocks']);
							}
							
						for ($x = 0; $x < $no_of_listings; $x++) {
// replace products prices with those from customers_group table
						  if (!empty($new_prices)) {
						    for ($i = 0; $i < count($new_prices); $i++) {
						      if ($product_info[$x]['products_id'] == $new_prices[$i]['products_id'] ) {
								  $product_info[$x]['products_price'] = $new_prices[$i]['products_price'];
                    for ($y = 1 ; $y < 9 ; $y++) {
                      $product_info[$x]['products_price'.$y.''] = $new_prices[$i]['products_price'.$y.''];
                      $product_info[$x]['products_price'.$y.'_qty'] = $new_prices[$i]['products_price'.$y.'_qty'];
                    }
                      $product_info[$x]['products_qty_blocks'] = $new_prices[$i]['products_qty_blocks'];
								  }
							  }
							} // end if(!empty($new_prices)
						} // end for ($x = 0; $x < $no_of_listings; $x++)
					} // end if ($this->cg_id != '0')

// an extra query is needed for all the specials

					$specials_query = tep_db_query("select products_id, specials_new_products_price from " . TABLE_SPECIALS . " where products_id in (" . $product_id_list . ") and status = '1' and customers_group_id = '" . $this->cg_id . "'");
					while ($specials_array = tep_db_fetch_array($specials_query)) {
						$new_s_prices[] = array ('products_id' => $specials_array['products_id'], 'specials_new_products_price' => $specials_array['specials_new_products_price']);
					}
	
// add the correct specials_new_products_price and replace final_price
					for ($x = 0; $x < $no_of_listings; $x++) {
					  if (!empty($new_s_prices)) {
					    for ($i = 0; $i < count($new_s_prices); $i++) {
					      if ($product_info[$x]['products_id'] == $new_s_prices[$i]['products_id'] ) {
					      $product_info[$x]['specials_new_products_price'] = $new_s_prices[$i]['specials_new_products_price'];
					      }
					    }
					  } // end if(!empty($new_s_prices)
						$this->addPriceBreakData($product_info[$x]['products_id'], $product_info[$x]);
					} // end for ($x = 0; $x < $no_of_listings; $x++)
			} // end if tep_not_null($product_id_list)
		} // end if (is_object($cart)
	}

	function addPriceBreakData ($products_id, $productinfo) {
		    $this->pricebreaks[$products_id] = array('products_id' => tep_get_prid($productinfo['products_id']),
				'products_price' => $productinfo['products_price'],
				'products_name' => $productinfo['products_name'],
				'products_model' => $productinfo['products_model'],
				'products_weight' => $productinfo['products_weight'],
				'products_quantity' => $productinfo['products_quantity'],
				'products_tax_class_id' => $productinfo['products_tax_class_id'],
				'products_image' => $productinfo['products_image'],
				'products_qty_blocks' => $productinfo['products_qty_blocks'],
				'specials_new_products_price' => $productinfo['specials_new_products_price'],
				'products_price1_qty' => $productinfo['products_price1_qty'],
				'products_price2_qty' => $productinfo['products_price2_qty'],
				'products_price3_qty' => $productinfo['products_price3_qty'],
				'products_price4_qty'=> $productinfo['products_price4_qty'],
				'products_price5_qty'=> $productinfo['products_price5_qty'],
				'products_price6_qty'=> $productinfo['products_price6_qty'],
				'products_price7_qty' => $productinfo['products_price7_qty'],
				'products_price8_qty' => $productinfo['products_price8_qty'],
				'products_price1' => $productinfo['products_price1'],
				'products_price2' => $productinfo['products_price2'],
				'products_price3'=> $productinfo['products_price3'],
				'products_price4' => $productinfo['products_price4'],
				'products_price5'=> $productinfo['products_price5'],
				'products_price6' => $productinfo['products_price6'],
				'products_price7' => $productinfo['products_price7'],
				'products_price8' => $productinfo['products_price8']);
	}
	
  function getPriceBreak($product_id) {
		$products_id = tep_get_prid($product_id);
		if(isset($this->pricebreaks[$products_id]) && tep_not_null($this->pricebreaks[$products_id])) {
			return $this->pricebreaks[$products_id];
			}	else {
				return false;
		}
	}
	
  function getStock($product_id) {
		$products_id = tep_get_prid($product_id);
		if(isset($this->pricebreaks[$products_id]) && tep_not_null($this->pricebreaks[$products_id])) {
			return $this->pricebreaks[$products_id]['products_quantity'];
			}	else {
				return false;
		}
	}

}
?>
